/*
***********************************************************************
Software License Agreement

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
***********************************************************************

Author:
Hisham Kedjour (hkedjour@gmail.com)
*/
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using Multitouch.Contracts;
using Application = System.Windows.Forms.Application;

namespace JooJooTouchProvider
{
	[AddIn("JooJoo", Publisher = "Hisham Kedjour", Description = "Provides input from JooJoo's eGalax Touch Panel", Version = "0.1.0")]
	[Export(typeof(IProvider))]
	public class JooJooInputProvider : IProvider
	{
		public static JooJooInputProvider InputProvider;

		private Thread _workerThread;

		public JooJooInputProvider()
		{
			InputProvider = this;
			
		}

		void WorkerThread()
		{
			var cb = new EGalaxTouch.EnumerateTouchscreenCallback(EnumerateTouchscreenCallback);

			EGalaxTouch.EnumerateTouchInstance(IntPtr.Zero, IntPtr.Zero, cb);
		}

		bool EnumerateTouchscreenCallback(IntPtr pContext, [MarshalAs(UnmanagedType.LPStr)]string szSymbolicName, uint nType)
		{
			IntPtr hDevice;
			
			hDevice = EGalaxTouch.CreateDevice(szSymbolicName);
			EGalaxTouch.EnableTouch(hDevice, false);
			EGalaxTouch.StartDeviceThread(hDevice);

			
			ulong branchInfo = 0;
			EGalaxTouch.GetBranchFormat(hDevice, ref branchInfo);
			branchInfo |= EGalaxTouch.BRANCH_ENABLED;
			EGalaxTouch.SetBranchFormat(hDevice, ref branchInfo);

			TouchHandlerForm frm = new TouchHandlerForm();
			EGalaxTouch.RegisterTPNotifyMessage(hDevice, frm.Handle, 0x0400 + 100);
			Application.Run(frm);


			EGalaxTouch.StopDeviceThread(hDevice);
			EGalaxTouch.EnableTouch(hDevice, true);
			EGalaxTouch.CloseDevice(hDevice);

			return true;
		}

		public void SendContact(Contact c)
		{
			EventHandler<NewFrameEventArgs> eventHandler = NewFrame;
			if (eventHandler != null)
				eventHandler(this, new NewFrameEventArgs(Stopwatch.GetTimestamp(), new[]{c}, null));
		}


		#region IProvider

		public void Start()
		{
			if (_workerThread != null)
				return;

			_workerThread = new Thread(WorkerThread);
			_workerThread.SetApartmentState(ApartmentState.STA);
			_workerThread.Start();
		}

		public void Stop()
		{
			if (_workerThread == null)
				return;

			Application.Exit();
			_workerThread.Join();
			_workerThread = null;
		}

		public bool IsRunning
		{
			get { return _workerThread != null; }
		}

		public bool HasConfiguration
		{
			get { return false; }
		}

		public UIElement GetConfiguration()
		{
			return null;
		}

		public bool SendImageType(ImageType imageType, bool value)
		{
			return false;
		}

		public bool SendEmptyFrames { get; set; }

		public event EventHandler<NewFrameEventArgs> NewFrame;

		#endregion
	}
}